var token = getCookie("token");
$.post('htz/user/token', { token, token }, function (res) {
    var userId = res.userId;
    layui.use(['form', 'layer', 'table', 'laydate'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var table = layui.table;
        var laydate = layui.laydate;
        var createDate1;
        var createDate2;
        laydate.render({
            elem: '#createDate1',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            theme: 'grid',
            done: function (value, date) {
                createDate1 = value;
            }
        });
        laydate.render({
            elem: '#createDate2',
            format: 'yyyy-MM-dd HH:mm:ss',
            type: 'datetime',
            theme: 'grid',
            done: function (value, date) {
                createDate2 = value;
            }
        });
        table.render({
            elem: '#test'
            , id: 'testReload'
            , height: 312
            , where: { isCheck: 1 }
            , url: 'jay/OutBound/getOutBoundList' //数据接口
            , even: true
            , parseData: function (res) {
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": res.obj.total, //解析数据长度
                    "data": res.obj.list //解析数据列表
                };
            }
            , toolbar: '#bar'
            , defaultToolBar: ['default', 'print', 'export']
            , page: true //开启分页
            , limit: 10
            , cols: [[ //表头
                { field: 'id', title: '序号', align: 'center', width: 80, sort: true, fixed: 'left' }
                , { field: 'typeName', title: '单据类型', align: 'center', width: 160 }
                , { field: 'createDate', title: '创建时间', align: 'center', width: 160 }
                , { field: 'obStatusName', title: '出库单状态', align: 'center', width: 200 }
                , { fixed: 'right', title: '操作', align: 'center', toolbar: '#barDemo' }
            ]]
        });
        var $ = layui.$, active = {
            reload: function () {
                table.reload('testReload', {
                    page: {
                        curr: 1//默认从第一页开始加载
                    },
                    where: {
                        type: $('#chukuType').val(),
                        status: $('#statusList').val(),
                        createDate1: createDate1,
                        createDate2: createDate2
                    }
                });
            }
        }
        $('#find').on('click', function () {
            var type = 'reload';
            active[type] ? active[type].call(this) : '';
        });
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event == 'detail') {
                layer.open({
                    type: 2,
                    area: ['600px', '380px'],
                    content: 'chukuDetail2.html',
                    success: function (layero, index) {
                        var iframe = window['layui-layer-iframe' + index];
                        //调用子页面的全局函数
                        iframe.relo(data.id);
                    }
                })
            }

            if (obj.event == "pass") {//通过审核
                if (data.status != 1) {
                    layer.msg("该明细已审核，请不要重复点击!", { icon: 2 });
                } else {
                    layer.confirm('<h3 style=\'text-align: center;color: green\'>是否审核</h3>', {
                        btn: ['确认审核', '取消审核']
                    }, function (index, layero) {
                        $.post('jay/OutBound/checkSuccess2', 'id=' + data.id + '&staffId='+userId, function (res) {
                            console.log(res);
                            if (res.obj != null || res.obj != 0) {
                                layer.msg("审核成功", { icon: 1 });
                                table.reload('testReload', {
                                    page: {
                                        curr: 1
                                    }
                                });
                            } else {
                                layer.msg("审核失败，请稍后再试!", { icon: 2 });
                                table.reload('testReload', {
                                    page: {
                                        curr: 1
                                    }
                                })
                            }
                        }, 'JSON');
                    }, function (index) {
                        layer.msg("您取消了操作", { icon: 2 })
                    })
                }
            } else if (obj.event == "pass") {//通过审核
                if (data.status != 1) {
                    layer.msg("该明细已审核，请不要重复点击!", { icon: 2 });
                } else {
                    layer.confirm('<h3 style=\'text-align: center;color: red\'>是否审核不通过</h3>', {
                        btn: ['确认审核不通过', '取消审核不通过']
                    }, function (index, layero) {
                        $.post('jay/OutBound/checkFail2', 'id=' + data.id + '&staffId='+userId+'&type=' + data.sourceId + '&type=' + data.type, function (res) {
                            if (res.obj != 0) {
                                layer.msg("审核不通过成功", { icon: 1 });
                                table.reload('testReload', {
                                    page: {
                                        curr: 1
                                    }
                                });
                            } else {
                                layer.msg("审核不通过失败，请稍后再试!", { icon: 2 });
                                table.reload('testReload', {
                                    page: {
                                        curr: 1
                                    }
                                });
                            }
                        }, 'JSON');
                    }, function (index) {
                        layer.msg("您取消了操作!", { icon: 2 })
                    })
                }
            }
        })
        $.ajax({
            url: 'jay/OutBound/getCheckStatus2',
            success: function (data) {
                var data = data.obj;
                for (var i = 0; i < data.length; i++) {
                    $("#statusList").append("<option value=\"" + data[i].id + "\">" + data[i].obStatusName + "</option>");
                }
                form.render("select");
            }
        });
        $.ajax({
            url: 'jay/OutBound/getTypeList',
            success: function (data) {
                var data = data.obj;
                for (var i = 0; i < data.length; i++) {
                    $("#chukuType").append("<option value=\"" + data[i].id + "\">" + data[i].typeName + "</option>");
                }
                form.render("select");
            }
        })
    });

})